/**
  ******************************************************************************
  * @file    main.c
  * @author  MCD Application Team
  * @version V1.1.0
  * @date    18-January-2013
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include "main.h"
#ifdef USE_KPU_BOARD
#include "uartStdio.h"
#else
#include "usbStdio.h"
#endif /* USE_KPU_BOARD */
#include "stm32f4_discovery.h"
#include "stm32f4xx_it.h"
#include "buffer.h"
#include "cmdline.h"
#include "devio.h"
#include "dev_gpio.h"
#include "dev_i2c.h"
#include "dev_spi.h"
#include "dev_iueab.h"
/** @addtogroup STM32F4xx_StdPeriph_Templates
  * @{
  */ 
extern int32_t i2cTimeout;
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define TIME_HANDLE_FREQ		100000

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
RCC_ClocksTypeDef    RCC_Clocks;

int32_t ledHandle;
int32_t buttonHandle;
int32_t iueabHandle;

/* Private function prototypes -----------------------------------------------*/

/* Private functions ---------------------------------------------------------*/
/**
  * @brief   main program
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       files (startup_stm32f40xx.s/startup_stm32f427x.s) before to branch to 
       application main. 
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f4xx.c file
     */  

  /* SysTick end of count event each 10ms */
  RCC_GetClocksFreq(&RCC_Clocks);
  // ---------- SysTick timer -------- //
  	// 168,000 Hz
  	if (SysTick_Config(SystemCoreClock/TIME_HANDLE_FREQ)) {
  		// Capture error
  		while (1){};
  	}

  /* Add your application code here */
#ifdef USE_KPU_BOARD
  	uartInit();
#else
  usbInit();
#endif /* USE_KPU_BOARD */

  deviceIoInit();

  int8_t buf[32];
  int32_t cmdStatus = 0;

  //EXTI_Config exti_config;

  registerCmdline((int8_t*)"help", cmdHelp, (int8_t*)"Help");

  /* Infinite loop */

  iueabHandle = devOpen((int8_t*)"iueab1", DEV_RDWR);
  /*
  exti_config.pinsource = EXTI_PinSource0;
  exti_config.mode =EXTI_Mode_Interrupt;
  exti_config.trigger = EXTI_Trigger_Rising;
  exti_config.func = EXTI0_ISR;
  exti_config.priority = 0x10;
  exti_config.subPriority = 0x11;
  */

  //devIoctl(buttonHandle, DEV_GPIO_CONFIG_INTERRUPT, &exti_config);

  //int16_t led = GPIO_Pin_12 | GPIO_Pin_13;


  //devIoctl(ledHandle, DEV_GPIO_CLEAR_BIT,&led);

  while (1)
  {
#ifdef USE_KPU_BOARD
	  uartPrintf((int8_t*)"\nThalamus>");
	  uartGets(buf, 128);
#else
	  usbPrintf((int8_t*)"\nThalamus>");
	  usbGets(buf, 128);
#endif /* USE_KPU_BOARD*/

	  cmdStatus = cmdLineProcess(buf);

	  if(cmdStatus == CMDLINE_BAD_CMD) {
#ifdef USE_KPU_BOARD
		  uartPrintf((int8_t*)"Bad Command!\n");
#else
		  usbPrintf((int8_t*)"Bad Command!\n");
#endif /* USE_KPU_BOARD*/
	  } else if(cmdStatus == CMDLINE_TOO_MANY_ARGS) {
#ifdef USE_KPU_BOARD
		  uartPrintf((int8_t*)"Too many arguments for command processor\n");
#else
		  usbPrintf((int8_t*)"Too many arguments for command processor\n");
#endif /* USE_KPU_BOARD */
	  }
	  else if(cmdStatus == CMDLINE_TOO_FEW_ARGS) {
#ifdef USE_KPU_BOARD
		  uartPrintf((int8_t*)"Too few arguments for command processor\n");
#else
		  usbPrintf((int8_t*)"Too few arguments for command processor\n");
#endif /* USE_KPU_BOARD */
	  	  }
  }
}


/**
  * @brief  Decrements the TimingDelay variable.
  */
void TimingDelay_Decrement(void)
{
	i2cTimeout--;
}


#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/**
  * @}
  */


/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
